<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-button type="primary" @click="showAddDialog" size="small"
          >添加分类</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格 -->
    <div class="tableBox">
      <el-table
        :data="tableData"
        height="100%"
        border
        style="width: 100%"
        stripe
      >
        <el-table-column type="index" width="50" align="center">
        </el-table-column>
        <el-table-column prop="menuname" label="分类名称" align="center">
        </el-table-column>
        <el-table-column prop="introduce" label="分类介绍" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scoped">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scoped.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="mini"
              style="color: red"
              icon="el-icon-delete"
              @click="handleDelete(scoped.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClose"
    >
      <!-- 添加分类的表单 -->
      <el-form :model="addCateForm" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.menuname"></el-input>
        </el-form-item>
        <el-form-item label="分类介绍">
          <el-input v-model="addCateForm.introduce"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddCateDialogConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="修改分类"
      :visible.sync="updateCateDialogVisible"
      width="50%"
    >
      <el-form ref="form" :model="updateform" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="updateform.menuname"></el-input>
        </el-form-item>
        <el-form-item label="分类介绍">
          <el-input v-model="updateform.introduce"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateCateDialogConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
function required() {
  return [{ required: true, message: "不能为空~", trigger: "blur" }];
}
export default {
  data() {
    return {
      rules: {
        name: required(),
      },
      // 添加分类对话框的状态控制
      addCateDialogVisible: false,
      tableData: [],
      updateCateDialogVisible: false,
      // 添加分类用到的表单
      addCateForm: {
        introduce: "",
        menuname: "",
      },
      updateform: {
        id: "",
        introduce: "",
        menuname: "",
        isDelete: undefined,
      },
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    handleDelete(row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.$http.get(`/banservice/firstmenu/deletemenu/${row.id}`);
          this.getTableData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async handleAddCateDialogConfirm() {
      if (!(this.addCateForm.menuname && this.addCateForm.introduce)) {
        return this.$message.warning("请输入完整");
      }
      this.$http({
        url: "/banservice/firstmenu/addfirstmenu",
        method: "post",
        data: {
          menuname: this.addCateForm.menuname,
          introduce: this.addCateForm.introduce,
        },
      }).then((res) => {
        this.$message.success("新增成功");
        this.updateCateDialogVisible = false;
      });
    },
    async handleUpdateCateDialogConfirm() {
      if (!(this.updateform.menuname && this.updateform.introduce)) {
        return this.$message.warning("请输入完整");
      }

      this.$http
        .post("/banservice/firstmenu/updatemenu", {
          ...this.updateform,
        })
        .then((res) => {
          this.$message.success("修改成功");
          this.updateCateDialogVisible = false;
        });
    },
    async getTableData() {
      const { data: res } = await this.$http.get(
        "/banservice/firstmenu/getfirstmenu"
      );
      this.tableData = res.data.list;
    },
    handleEdit(row) {
      this.updateCateDialogVisible = true;
      this.updateform = row;
      console.log(this.updateform);
    },

    // 点击添加分类按钮
    async showAddDialog() {
      this.addCateDialogVisible = true;
    },

    // 点击添加分类中的确定按钮
    addCate() {},
    // 对话框的关闭事件
    addCateDialogClose() {},
  },
};
</script>
<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;
}
.el-cascader {
  width: 100%;
}
.tableBox {
  height: 63vh;
}
</style>
